package com.china08.yxyapi.api.home.activity.weike;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.huodong.YxyHuodongWeikeContest;
import com.china08.yxyapi.entity.db.huodong.YxyHuodongWeikeContestCatalogue;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyHuodongWeikeContestCatalogueRepository;
import com.china08.yxyapi.repository.db.YxyHuodongWeikeContestRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

@CrossOrigin
@RestController
public class ActivitySignController extends YxyApiBaseController {

    @Autowired
    private YxyHuodongWeikeContestCatalogueRepository yxyHuodongWeikeContestCatalogueRepository;

    @Autowired
    private YxyHuodongWeikeContestRepository yxyHuodongWeikeContestRepository;

    @RequestMapping(value = "/api_yw/home/activity/tea/sign", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, //
            HttpServletResponse response//
    ) {
        YxyProfile profile = getProfile(request, response);
        YxyHuodongWeikeContest res = yxyHuodongWeikeContestRepository.findByUserId(profile.getId());
        if (res == null) {
            res = new YxyHuodongWeikeContest();
            res.setUserId(profile.getId());
            res.setSchoolid(profile.getSchoolId());
            res.setSchoolNick(profile.getSchoolName());
            res.setUserNick(profile.getUserNick());
            yxyHuodongWeikeContestRepository.save(res);
        }
        List<YxyHuodongWeikeContestCatalogue> list = yxyHuodongWeikeContestCatalogueRepository
                .findByUserIdOrderByCreatedDateAsc(profile.getId());
        List<YxyHuodongWeikeContestCatalogue> list2 = new ArrayList<YxyHuodongWeikeContestCatalogue>();
        Integer count = 0;
        String mes = "";
        for (YxyHuodongWeikeContestCatalogue yxyHuodongWeikeContestCatalogue : list) {
            yxyHuodongWeikeContestCatalogue.setApply(true);
            count = yxyHuodongWeikeContestCatalogueRepository
                    .countByBookContentIdAndIsApply(yxyHuodongWeikeContestCatalogue.getBookContentId(), true);
            if (count > 5) {
                mes = yxyHuodongWeikeContestCatalogue.getPhaseName() + "-"
                        + yxyHuodongWeikeContestCatalogue.getSubjectName() + "-"
                        + yxyHuodongWeikeContestCatalogue.getVersionName() + "-"
                        + yxyHuodongWeikeContestCatalogue.getSemesterName() + "-"
                        + yxyHuodongWeikeContestCatalogue.getKnowNames();
                break;
            }
            list2.add(yxyHuodongWeikeContestCatalogue);
        }
        RespModel respModel = new RespModel();

        if (StringUtils.isBlank(mes)) {
            yxyHuodongWeikeContestCatalogueRepository.save(list2);
            respModel.setMessage("成功");
        } else {
            Assert.isTrue(false, mes + "已被其他老师报名了,请选择其他章节");
        }
        return respModel;
    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
